package org.example.redis.controller;

import org.example.redis.entity.Member;
import org.example.redis.repository.MemberRepository;
import org.example.redis.service.MemberService;
import org.example.redis.utils.JsonResult;
import org.example.redis.utils.Md5Utils;
import org.example.redis.utils.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("member")
public class MemberController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private MemberService memberService;
    @Autowired
    private Member member;
    @Autowired
    private MemberRepository memberRepository;

    //获取验证码接口222222
    @GetMapping("get_sms_code")
    public JsonResult getSmsCode(@RequestParam("mobile") String mobile) {
        //手机号码格式验证
        if(!Tool.isMobile(mobile)) {
            return JsonResult.failed("手机号码格式不正确.");
        }
        //生成4位的格式是数字的验证码
        // 创建Random实例
        Random random = new Random();
        // 生成1000-9999之间的随机数（包含边界）
        int randomNum = random.nextInt(9000) + 1000;
        //保存到redis,有效期:60s
        String key = "sms_code_" + mobile;
        stringRedisTemplate.opsForValue().set(key, String.valueOf(randomNum),60, TimeUnit.SECONDS);
        return JsonResult.success("获取验证码成功,",randomNum);
    }

    //会员注册接口
    @PostMapping("register")
    public JsonResult register(@RequestParam("mobile") String mobile,
                               @RequestParam("code") String code,
                               @RequestParam("name") String name,
                               @RequestParam("password") String password
    ) {
        //验证码校验
        String key = "sms_code_" + mobile;
        String smsCode = stringRedisTemplate.opsForValue().get(key);
        if (smsCode == null || !smsCode.equals(code)) {
            return JsonResult.failed("验证码输入错误");
        }
        //清除redis验证码
        stringRedisTemplate.delete(key);
        //校验手机号码重复
        Member resultMobile = memberService.findByMobile(mobile);
        if(resultMobile != null) {
            return JsonResult.failed("该手机号码已经注册，请更换...");
        }
        //校验账号码重复
        Member resultName = memberService.findByMobile(mobile);
        if(resultName != null) {
            return JsonResult.failed("该账号已经注册，请更换...");
        }
        //md5加密处理
        String safePassword = Md5Utils.encrypt(password);
        member.setPassword(safePassword);
        member.setName(name);
        member.setMobile(mobile);
        Member res = memberRepository.save(member);
        if(res == null) {
            return JsonResult.failed("注册失败");
        }
        return JsonResult.success("注册成功",null);
    }
    //登录接口
    @PostMapping("login")
    public JsonResult login(@RequestParam("mobile") String mobile,
                            @RequestParam("password") String password) {

        if (!Tool.isMobile(mobile)) {
            return JsonResult.failed("登录失败，手机号码不能为空");
        }
        Member result = memberService.findByMobile(mobile);
        if(result == null || !Md5Utils.encrypt(password).equals(result.getPassword())) {
            return JsonResult.failed("登录失败，密码或账号输入错误");
        }
        return JsonResult.success("登录成功",null);
    }
    //11111132222
}

